import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'

const baseUrl = CommonConfig.bmsUrl + '/v1'

// 获取字典组
export function getDictgroup () {
  const newUrl = baseUrl + '/dict_groups'

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 根据字典组代码获取字典
 * code 字典组代码
 * **/
export function getDictlistByCode (code) {
  const newUrl = baseUrl + `/dict_group/${code}/dictionaries`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 添加字典
 * code 字典组代码
 * enableFlag 是否可用
 * label 字典内容
 * remark 备注
 * value 字典值
 * **/
export function addDict (code, params) {
  const newUrl = baseUrl + `/dict_group/${code}/dictionaries`

  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 编辑字典
 * id 字典id
 * enableFlag 是否可用
 * label 字典内容
 * remark 备注
 * value 字典值
 * **/
export function editDict (id, params) {
  const newUrl = baseUrl + `/dictionaries/${id}`

  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 排序
 * id1
 * id2
 * **/
export function sortDict (params) {
  const newUrl = baseUrl + `/dictionaries/sort`

  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 删除字典
 * id 字典id
 * **/
export function delDict (id) {
  const newUrl = baseUrl + `/dictionaries/${id}`

  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 删除字典
 * id 字典id
 * enableFlag 启用禁用 true/false
 * **/
export function enableDict (id, params) {
  const newUrl = baseUrl + `/dictionaries/${id}/enable`

  return axiosInstance.patch(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}
